﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace DataProcessing
{
    public class Settings
    {
        static private string SETTING_FILE = "settings.xml";

        static public string SettingFile
        {
            get { return SETTING_FILE; }
            set { SETTING_FILE = value; }
        }

        public int MaxOffset
        {
            get;
            set;
        }
        static Settings _instance;

        [XmlIgnore]
        public static Settings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Load();
                }
                return _instance;
            }

        }
        public static void Reset()
        {
            _instance = null;

        }
        public int ChromeCount
        {
            get { return ChromeSettingList.Count; }
        }

        [XmlIgnore]
        public Dictionary<String, ChromeSetting> ChromeSettings
        {
            get;set;
        }

        public List<ChromeSetting> ChromeSettingList
        {
            get;
            set;
        }


        
        public static Settings Load()
        {
            Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory;          
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            FileStream fs = new FileStream(SETTING_FILE, FileMode.Open);
            TextReader reader = new StreamReader(fs);
            Settings ret = serializer.Deserialize(reader) as Settings;
            ret.ChromeSettings = new Dictionary<string, ChromeSetting>();

            ChromeSetting[] tmpList = new ChromeSetting[ret.ChromeCount];
            
            //加入字典，并且按照index字段指示的顺序存储。
            foreach (ChromeSetting chromeSetting in ret.ChromeSettingList)
            {
                ret.ChromeSettings.Add(chromeSetting.Name.ToLower(), chromeSetting);
                tmpList[chromeSetting.Index] = chromeSetting;
                
            }

            ret.ChromeSettingList.Clear();
            ret.ChromeSettingList.AddRange(tmpList);
            
            fs.Close();
            ret.MaxOffset = 0;
            ret.CalculateAlignPoint();
            return ret;
        }

        /// <summary>
        /// 有些物种没有中央对齐方式的数据
        /// </summary>
        /// <returns></returns>
        public bool HasAlighPoint()
        {
            var tmp = from setting in ChromeSettingList
                      where setting.AlignmentPoint != 0
                      select setting;
            if (tmp != null && tmp.Count() != 0)
                return true;

            return false;
        }
        /// <summary>
        /// from easy to read to ease for use.
        /// </summary>
        public void CalculateAlignPoint()
        {
          
          
            var tmp =   from setting in  ChromeSettingList
                        orderby setting.AlignmentPoint descending
                        select setting;

            List<ChromeSetting> tmpList= tmp.ToList();
            foreach (ChromeSetting setting in tmpList)
            {
                setting.Offset = (int)(tmpList[0].AlignmentPoint - setting.AlignmentPoint);
                MaxOffset = Math.Max(MaxOffset, setting.Offset);
            }

        }


        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            FileStream fs = new FileStream("sample"+SETTING_FILE, FileMode.Create);
            TextWriter writer = new StreamWriter (fs);
            serializer.Serialize(writer,this);
            fs.Close();
        }
        public static void GenerateSampleFile()
        {
            Settings setting = new Settings();

            ChromeSetting tmp = new ChromeSetting();
            tmp.AlignmentPoint = 0;
            tmp.Index = 0;
            tmp.Name = "0";
            setting.ChromeSettings = new Dictionary<string, ChromeSetting>();
            setting.ChromeSettings.Add(tmp.Name, tmp);
            setting.ChromeSettingList = setting.ChromeSettings.Values.ToList();
            setting.Save();
                 
        }
    }
}
